package com.yanqu.road.server.cmd.activity.stronghold.wild;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.strongholdwar.StrongholdModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdActivityMgr;
import com.yanqu.road.server.manger.system.CommonMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;

import java.math.BigInteger;

@Cmd(code = GameProtocol.S_STRONGHOlD_USE_ITEN_RECOVER_POWER, desc = "野外据点使用道具恢复体力")
public class UseItemRecoverPowerCmd extends AbstractCommand {
    private int num;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        StrongholdWarProto.StrongholdWildRecoverPowerReqMsg req = StrongholdWarProto.StrongholdWildRecoverPowerReqMsg.parseFrom(packet.getBytes());
        num = req.getNum();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = StrongholdActivityMgr.getOpenActivityInfo();
        if (activityInfo == null) {
            ret = GameErrorCode.E_STRONGHOLD_NOT_FOUND;
            return;
        }
        int activityId = activityInfo.getActivityId();
        int itemId = StrongholdActivityMgr.getPowerItemId();
        GoodsInfo goodsInfo = CommonMgr.getGoodsInfo(itemId);
        if (goodsInfo == null) {
            ret = GameErrorCode.E_BAG_ITEM_NO_EXIST;
            return;
        }
        if (!player.getModule(CurrencyModule.class).removeCurrency(new Property(itemId, BigInteger.valueOf(num)), eLogMoneyType.Stronghold, eLogMoneyType.StrongholdRecoverPower)) {
            ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            return;
        }
        int recoverNum = goodsInfo.getParamList().get(0).intValue() * num;
        player.getModule(StrongholdModule.class).addPower(activityId, recoverNum);


    }

    @Override
    public void sendData(GamePlayer player) {
        StrongholdWarProto.StrongholdWildRecoverPowerRespMsg.Builder resp = StrongholdWarProto.StrongholdWildRecoverPowerRespMsg.newBuilder();
        resp.setRet(ret);
        player.sendPacket(ClientProtocol.U_STRONGHOlD_USE_ITEN_RECOVER_POWER, resp);
    }
}
